#ifndef RESPONSIVEFILELISTENGINE_H
#define RESPONSIVEFILELISTENGINE_H

#include <QWidget>
#include <QResizeEvent>
#include <QPropertyAnimation>
#include <QEasingCurve>
#include <QTimer>
#include <QParallelAnimationGroup>
#include <QSequentialAnimationGroup>
#include <QGraphicsEffect>
#include <QGraphicsDropShadowEffect>
#include <QLinearGradient>
#include <QRadialGradient>
#include "unifiedfileinfo.h"

/**
 * 🎨 Sony Ive 响应式文件列表设计引擎
 *
 * 核心设计哲学：渐进式信息显示 (Progressive Information Disclosure)
 * - 基于可用空间智能调整信息密度
 * - 避免信息过载，优先显示最重要的内容
 * - 用户体验随容器宽度平滑过渡
 *
 * 优雅的视觉过渡 (Seamless Visual Transitions)
 * - Sony风格的微妙渐变和阴影
 * - 流畅的属性动画系统
 * - 触觉反馈式的交互响应
 */

namespace SonyIveDesign {

// === 响应式断点系统 ===
enum class DisplayMode {
    Micro = 0,      // < 180px: 图标 + 超简文件名
    Compact = 1,    // 180-250px: + 状态指示
    Standard = 2,   // 250-350px: + 进度 + 页码
    Expanded = 3,   // 350-450px: + 布局模式
    Full = 4        // > 450px: 完整信息展示
};

// === Sony设计语言常量 ===
struct DesignConstants {
    // 视觉元素
    static constexpr int BORDER_RADIUS = 8;
    static constexpr int SHADOW_BLUR_RADIUS = 12;
    static constexpr int SHADOW_OFFSET = 2;
    static constexpr double HOVER_SCALE = 1.05;
    static constexpr double SELECTION_SCALE = 1.02;

    // 图标尺寸策略
    static constexpr int ICON_SIZE_LARGE = 32;   // Full/Expanded
    static constexpr int ICON_SIZE_MEDIUM = 24;  // Standard
    static constexpr int ICON_SIZE_SMALL = 16;   // Compact/Micro

    // 动画参数
    static constexpr int ANIMATION_DURATION_FAST = 150;
    static constexpr int ANIMATION_DURATION_NORMAL = 250;
    static constexpr int ANIMATION_DURATION_SLOW = 350;

    // 间距和边距
    static constexpr int SPACING_MICRO = 2;
    static constexpr int SPACING_SMALL = 4;
    static constexpr int SPACING_NORMAL = 8;
    static constexpr int SPACING_LARGE = 12;

    // 响应式断点
    static constexpr int BREAKPOINT_MICRO = 180;
    static constexpr int BREAKPOINT_COMPACT = 250;
    static constexpr int BREAKPOINT_STANDARD = 350;
    static constexpr int BREAKPOINT_EXPANDED = 450;
};

// === 智能文本处理策略 ===
class AdaptiveTextProcessor {
public:
    // 智能省略，保留文件名关键部分
    static QString getSmartEllipsis(const QString& text, int maxWidth, const QFont& font);

    // 移除扩展名节省空间（除Full模式）
    static QString getFileNameWithoutExtension(const QString& fileName, DisplayMode mode);

    // 动态字体大小调整
    static QFont getAdaptiveFont(DisplayMode mode, bool isBold = false);

    // 计算文本所需宽度
    static int calculateTextWidth(const QString& text, const QFont& font);
};

// === Sony风格颜色管理 ===
class SonyColorPalette {
public:
    // Sony风格渐变背景
    static QLinearGradient createSonyGradient(const QRect& rect, bool isSelected = false, bool isHovered = false);

    // 状态颜色
    static QColor getStatusColor(FileProcessStatus status, double opacity = 1.0);

    // 文本颜色（自适应主题）
    static QColor getTextColor(bool isPrimary = true, double opacity = 1.0);

    // 边框颜色
    static QColor getBorderColor(bool isSelected = false, double opacity = 0.3);

    // 阴影颜色
    static QColor getShadowColor(double opacity = 0.2);
};

} // namespace SonyIveDesign

/**
 * 响应式文件列表引擎
 *
 * 职责：
 * 1. 检测容器宽度变化并计算DisplayMode
 * 2. 管理响应式动画系统
 * 3. 提供Sony设计语言的视觉效果
 * 4. 优化性能和内存使用
 */
class ResponsiveFileListEngine : public QObject
{
    Q_OBJECT
    Q_PROPERTY(double animationOpacity READ animationOpacity WRITE setAnimationOpacity)
    Q_PROPERTY(double hoverScale READ hoverScale WRITE setHoverScale)
    Q_PROPERTY(double selectionScale READ selectionScale WRITE setSelectionScale)

public:
    explicit ResponsiveFileListEngine(QWidget* parentWidget);
    ~ResponsiveFileListEngine() override = default;

    // === 响应式模式检测 ===
    SonyIveDesign::DisplayMode calculateDisplayMode(int containerWidth) const;
    SonyIveDesign::DisplayMode getCurrentDisplayMode() const { return m_currentDisplayMode; }

    // === 布局计算 ===
    int calculateOptimalColumns(int containerWidth) const;
    int calculateItemHeight(SonyIveDesign::DisplayMode mode) const;
    QSize calculateItemSize(SonyIveDesign::DisplayMode mode) const;

    // === 动画系统 ===
    void animateToDisplayMode(SonyIveDesign::DisplayMode newMode);
    void animateHoverEffect(QWidget* target, bool isHovering);
    void animateSelectionEffect(QWidget* target, bool isSelected);

    // === Sony设计语言效果 ===
    void applySonyDropShadow(QWidget* target);
    void removeSonyDropShadow(QWidget* target);
    QPixmap createSonyBackgroundTexture(const QSize& size, bool isSelected = false, bool isHovered = false);

    // === 性能优化 ===
    void enablePerformanceMode(bool enabled);
    void clearCache();

    // === 属性访问器 ===
    double animationOpacity() const { return m_animationOpacity; }
    void setAnimationOpacity(double opacity);

    double hoverScale() const { return m_hoverScale; }
    void setHoverScale(double scale);

    double selectionScale() const { return m_selectionScale; }
    void setSelectionScale(double scale);

signals:
    void displayModeChanged(SonyIveDesign::DisplayMode newMode, SonyIveDesign::DisplayMode oldMode);
    void layoutUpdateRequired();
    void animationFinished();

public slots:
    void onContainerResized(const QSize& newSize);
    void onThemeChanged();

private slots:
    void onModeTransitionFinished();

private:
    void initializeAnimationSystem();
    void setupPerformanceTimer();
    void createDropShadowEffect();

    // === 核心成员变量 ===
    QWidget* m_parentWidget;
    SonyIveDesign::DisplayMode m_currentDisplayMode;
    SonyIveDesign::DisplayMode m_targetDisplayMode;

    // === 动画系统 ===
    QPropertyAnimation* m_opacityAnimation;
    QPropertyAnimation* m_hoverAnimation;
    QPropertyAnimation* m_selectionAnimation;
    QParallelAnimationGroup* m_transitionGroup;

    // === 视觉效果 ===
    QGraphicsDropShadowEffect* m_dropShadowEffect;

    // === 性能优化 ===
    QTimer* m_performanceTimer;
    bool m_isPerformanceModeEnabled;
    QHash<QString, QPixmap> m_backgroundCache;

    // === 动画属性 ===
    double m_animationOpacity;
    double m_hoverScale;
    double m_selectionScale;

    // === 状态管理 ===
    bool m_isTransitioning;
    int m_lastContainerWidth;
};

#endif // RESPONSIVEFILELISTENGINE_H